package Servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.UserDAO;
import bean.User;

public class UserListServlet extends HttpServlet {
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		
		List<User> users = new UserDAO().list();
		
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("<table align='center' border='1' cellspacing='0'>\r\n");
		stringBuffer.append("<tr><td>id</td><td>username</td><td>password</td><td>edit</td><td>delete</td></tr>\r\n");
		
		String trForamt = "<tr><td>%d</td><td>%s</td><td>%s</td><td><a href='editUser?id=%d'>edit</a></td><td><a href='deleteUser?id=%d'>delete</a></td></tr>\r\n";
		
		
		for(User user : users) {
			String tr = String.format(trForamt, user.getId(),user.getUsername(),user.getPassword(),user.getId(),user.getId());
			stringBuffer.append(tr);
		}
		stringBuffer.append("</table>");
		response.getWriter().write(stringBuffer.toString());
	}
}
